package gmwxapplet

import (
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/devices"
	"yundian/internel/app/models/scanlogs"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// HandleInsertScanLogs 新增扫码日志
func HandleInsertScanLogs(ctx *gin.Context) {
	param := &request.ScanLogInsertReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	m := &scanlogs.ScanLogsModel{ }
	// 获取openid
	var openid string
	wc := logic.NewSettingLogic().GetPaySetting()
	respData, _ := payClient.Login(param.Code, wc.AppId, wc.AppSecret)
	if respData != nil { openid = respData.Get("openid") }

	// 获取设备信息
	devInfo := dao.NewDeviceDao().GetDeviceInfoWithPlace(param.DeviceSn).Unwrap().(*devices.DeviceWithPlace)
	m.PlaceId = devInfo.PlaceId
	m.PlaceName = devInfo.PlaceName
	m.ViewIp = ctx.ClientIP()
	m.CreateAt = dingo.Time(time.Now().Unix())
	m.PagePath = param.PagePath
	m.DeviceSn = param.DeviceSn
	m.SenceId = param.SenceId
	m.SenceDesc = helper.GetWeixinSence(param.SenceId)
	m.Openid = openid

	Response(ctx)("", CodeInsertSuccess, dao.NewScanLogsDao().InsertScanLogs(m).Unwrap())(OK)
}

